﻿#region

using System;
using System.Windows;
using System.Windows.Controls;
using Radovici.SharedLibrary;
using Debug=System.Diagnostics.Debug;

#endregion

namespace Radovici.Ctrls.ContentControls
{
    public class Widget : ContentControl
    {
        #region Private fields

        private Button _cancelButton;
        private Button _okButton;
        private IDisposable _prevContent;
        private TextBlock _titlePart;

        #endregion

        public Widget()
        {
            DefaultStyleKey = typeof (Widget);
        }

        public bool OkEnabled { get; set; }

        #region IsModal

        // Using a DependencyProperty as the backing store for IsModal.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsModalProperty = DependencyProperty.Register("IsModal", typeof (bool),
                                                                                                typeof (Widget),
                                                                                                new PropertyMetadata(
                                                                                                    true));

        public bool IsModal
        {
            get { return (bool) GetValue(IsModalProperty); }
            set { SetValue(IsModalProperty, value); }
        }

        #endregion

        #region Title

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof (string),
                                                                                              typeof (Widget),
                                                                                              new PropertyMetadata(
                                                                                                  string.Empty,
                                                                                                  OnTitleChanged));

        public string Title
        {
            get { return (string) GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        private static void OnTitleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ctrl = d as Widget; //TODO: _titlePart isn't loaded by the time the Title DP is set.
            if (ctrl != null && ctrl._titlePart != null)
            {
                ctrl._titlePart.Text = e.NewValue as string;
            }
        }

        #endregion

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (_prevContent != null)
            {
                _prevContent.Dispose();
            }
            _prevContent = Content as IDisposable;

            _titlePart = GetTemplateChild("TitlePart") as TextBlock;
            //NOTE: Initialize TitlePart because it the dependency property is set before OnApplyTemplate (empirically - need to understand this lifecycle better)
            _titlePart.Text = Title;
            Debug.Assert(_titlePart != null, "Widget must have a title part");
            var close = GetTemplateChild("CloseButton") as Button;
            if (close != null)
            {
                close.Click += delegate { if (Closed != null) Closed(this, new CancelEventArgs(true)); };
            }
            var pnl = GetTemplateChild("ButtonPanel") as Panel;
            if (pnl != null)
            {
                if (OkEnabled)
                {
                    var btn = new Button {Height = 18, Width = 50, Content = "Ok"};
                    btn.Click += delegate { if (Closed != null) Closed(this, new CancelEventArgs(false)); };
                    pnl.Children.Add(btn);
                }
            }
            //_okButton = this.GetTemplateChild("OkButton") as Button;
            //Debug.Assert(_okButton != null, "Widget must have a ok button (although it may be invisible)");
            //_okButton.Visibility = OkCallback != null ? Visibility.Visible : Visibility.Collapsed;
            //_cancelButton = this.GetTemplateChild("CancelButton") as Button;
            //Debug.Assert(_cancelButton != null, "Widget must have a cancel button (although it may be invisible)");
            //_cancelButton.Visibility = _cancelButton != null ? Visibility.Visible : Visibility.Collapsed;
        }

        public event CancelEventHandler Closed;
    }
}